package cn.tedu.truck.start.web;

import java.io.Serializable;

import cn.tedu.truck.start.exception.ServiceException;
import lombok.Data;

@Data
public class JsonResult<T> implements Serializable {

    private Integer state;
    private String message;
    private T data;

    public static JsonResult ok() {
        return ok(null);
    }
    
    public static JsonResult ok(Object data) {

        JsonResult result = new JsonResult();

        result.state = ServiceCode.OK.getCode();
        result.message = null;
        result.data = data;
        return result;
    }

    public static JsonResult fail(ServiceException e) {
        return fail(e.getServiceCode(), e.getMessage());
    }

    public static JsonResult fail(ServiceCode code, String message) {
        JsonResult result = new JsonResult();

        result.state = code.getCode();
        result.message = message;
        return result;
    }
}
